/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.vijay.mvnbuilddownload;

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 *
 * @author elangovanv
 */
public class FileLocator {
    
    private String sourceURL;
    private String downloadVersion;
    private String productName;
    private String extension;
    private boolean noversion;

    private String fileName="";
    
    private String finalUrl;

    public FileLocator(String sourceURL, String productName, String downloadVersion, String extension, boolean noversion) {
        this.downloadVersion=downloadVersion;
        this.sourceURL=sourceURL+"/"+downloadVersion;
        this.productName=productName;
        this.extension=extension;
        this.noversion=noversion;
    }

    public String locate() {

        if (downloadVersion.contains("SNAPSHOT")) {
            return locateSnapshot();
        } else {
            return locateRelease();
        }
    }

    public String locateRelease() {
        StringBuffer sb=new StringBuffer();
        sb.append(productName).append("-").append(downloadVersion).append(".").append(extension);

        String finalUrl=sb.toString();
        //fileName=sb.toString();
        if (!noversion) {
            fileName=productName+"-"+downloadVersion+"."+extension;
        } else {
            fileName=productName+"."+extension;
        }

        sb =new StringBuffer(sourceURL);
        sb.append("/").append(finalUrl);

        return sb.toString();
    }

    public String locateSnapshot() {
        XMLUtil xml = new XMLUtil();
        Metadata metadata=null;
        try {
            Document ldocument = xml.parseXML(sourceURL, Constant.MVN_VERSION_FILE);

            MetadataParser parser=MetadataParser.getMetadataParser(ldocument);
            metadata=parser.parse();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        }

        //System.out.println("metadata.getArtifactId().equals(productName) :"+metadata.getArtifactId().equals(productName));
        if (metadata.getArtifactId().equals(productName)) {
            StringBuffer sb =new StringBuffer(sourceURL);
            sb.append("/").append(productName);

            if (downloadVersion.contains("SNAPSHOT")) {
                sb.append("-").append(downloadVersion.replace("-SNAPSHOT", ""));
                sb.append("-").append(metadata.getVersioning().getSnapshotVersioning().getTimestamp()).append("-").append(metadata.getVersioning().getSnapshotVersioning().getBuildNumber());
                //System.out.println("sb output :"+sb.toString());
                //fileName=productName+"-"+downloadVersion.replace("-SNAPSHOT", "")+"."+extension;
                if (!noversion) {
                    fileName=productName+"-"+downloadVersion.replace("-SNAPSHOT", "")+"."+extension;     
                } else {
                    fileName=productName+"."+extension;     
                }
            } 

            sb.append(".").append(extension);

            finalUrl =sb.toString();            
        }
//System.out.println("final url ; "+finalUrl);
        return finalUrl;
    }

    public String getFileName() {
        return fileName;
    }
}
